package com.wyutil.wy_FileFormatConversion.CsvWithExcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

public class CsvToExcel {
    @Test
    public void test1() throws IOException {
        convertCsvToExcel(new File("C:\\Users\\Administrator\\Downloads\\20240114134120.csv"),
                "C:\\Users\\Administrator\\Downloads\\20240114134120.xls");
    }
    public void convertCsvToExcel(File csvFile, String excelFile) throws IOException {
        File file = new File(excelFile);
        // 使用EasyExcel读取csv文件
        ExcelReaderBuilder readerBuilder = EasyExcel.read(csvFile).charset(Charset.forName("GBK"));
        //指定从第几行开始读取，默认情况下会把第一行当做表头忽略读取，会少一行的数据，参数0从第一行开始读取数据
        readerBuilder.headRowNumber(0);
        List<Object> dataList = readerBuilder.sheet().doReadSync();
        // 使用EasyExcel写入xlsx文件
        ExcelWriterBuilder writerBuilder = EasyExcel.write(file);
        writerBuilder.sheet().doWrite(dataList);
    }
}
